package com.david.IfaceAutoTest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.david.IfaceAutoTest.domain.InterfaceDomain;
import com.david.IfaceAutoTest.domain.TableDomain;
import com.david.IfaceAutoTest.entity.*;
import com.david.IfaceAutoTest.mapper.*;
import com.david.IfaceAutoTest.service.InterfaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.david.IfaceAutoTest.utils.RedisUtils;
import com.david.IfaceAutoTest.utils.RestUtil;
import jdk.nashorn.internal.IntDeque;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author david
 * @since 2021-12-14
 */
@Service
public class InterfaceServiceImpl extends ServiceImpl<InterfaceMapper, Interface> implements InterfaceService {
    @Resource
    InterfaceMapper interfaceMapper;
    @Resource
    InterfaceHeaderMapper interfaceHeaderMapper;
    @Resource
    InterfaceQueryMapper interfaceQueryMapper;
    @Resource
    InterfaceBodyMapper interfaceBodyMapper;
    @Resource
    InterfaceEventMapper interfaceEventMapper;
    @Resource
    ProjectMapper projectMapper;
    @Resource
    ProjectModelMapper projectModelMapper;
    @Resource
    RestUtil restUtil;

    @Override
    public String run(Integer interfaceId) {

        //拼接结果，调用rest工具类执行接口。
        InterfaceDomain interfaceDomain = getInterfaceDomain(interfaceId);
        return restUtil.getResult(interfaceDomain);
    }

    @Override
    public String getResult(Integer interfaceId) {

        //拼接结果，调用rest工具类执行接口。
        InterfaceDomain interfaceDomain = getInterfaceDomain(interfaceId);
        return restUtil.getResult(interfaceDomain);
    }

    @Override
    public InterfaceDomain getInterfaceDomain(Integer interfaceId) {
        InterfaceDomain interfaceDomain = new InterfaceDomain();
        Interface anInterface = interfaceMapper.selectById(interfaceId);
        interfaceDomain.setId(anInterface.getId());
        ProjectModel projectModel = projectModelMapper.selectOne(new QueryWrapper<ProjectModel>().select("project_id").eq("id", anInterface.getModelId()));
        interfaceDomain.setProjectId(projectModel.getProjectId());
        interfaceDomain.setDescription(anInterface.getDescription());
        interfaceDomain.setHost(anInterface.getHost());
        interfaceDomain.setInterfaceMethod(anInterface.getInterfaceMethod());
        interfaceDomain.setInterfaceName(anInterface.getInterfaceName());
        interfaceDomain.setInterfacePath(anInterface.getInterfacePath());
        interfaceDomain.setInterfaceProtocol(anInterface.getInterfaceProtocol());
        interfaceDomain.setInterfaceRaw(anInterface.getInterfaceRaw());

        interfaceDomain.setInterfaceBodies(interfaceBodyMapper.selectList(new QueryWrapper<InterfaceBody>().eq("interface_id", anInterface.getId())));
        interfaceDomain.setInterfaceEvents(interfaceEventMapper.selectList(new QueryWrapper<InterfaceEvent>().eq("interface_id", anInterface.getId())));
        interfaceDomain.setInterfaceHeaders(interfaceHeaderMapper.selectList(new QueryWrapper<InterfaceHeader>().eq("interface_id", anInterface.getId())));
        interfaceDomain.setInterfaceQueries(interfaceQueryMapper.selectList(new QueryWrapper<InterfaceQuery>().eq("interface_id", anInterface.getId())));
        return interfaceDomain;
    }

    @Override
    public List<TableDomain> getTableDomain(String tableName) {
        return interfaceMapper.getTableDomain(tableName);
    }

    @Override
    public List<Map<String, Object>> getTest() {
        return interfaceMapper.getTest();
    }


}
